package com.ai.demo.picture;

/**
 * AUTHER: wangyue
 * TIME  : 2025/3/17 : 16:15
 */
import javax.imageio.ImageIO;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

public class ImageConcat {
    public static void main(String[] args) {
        try {
            // 加载图片
            BufferedImage image1 = ImageIO.read(new File("E:\\HuaweiMoveData\\Users\\wangyue\\Desktop\\jiajiao\\111.jpg"));
            BufferedImage image2 = ImageIO.read(new File("E:\\HuaweiMoveData\\Users\\wangyue\\Desktop\\jiajiao\\banner3.jpg"));
//            BufferedImage image3 = ImageIO.read(new File("E:\\HuaweiMoveData\\Users\\wangyue\\Desktop\\jiajiao\\banner3.jpg"));

            // 确定拼接方式（水平或垂直）
            boolean horizontal = false; // 设置为true为水平拼接，false为垂直拼接

            // 计算新图片的宽度和高度
            int width = horizontal ? image1.getWidth() + image2.getWidth() : Math.max(image1.getWidth(), image2.getWidth());
            int height = horizontal ? Math.max(image1.getHeight(), image2.getHeight()) : image1.getHeight() + image2.getHeight();

            // 创建新的BufferedImage对象
            BufferedImage combinedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
            Graphics2D g2d = combinedImage.createGraphics();

            // 绘制图片到新图片上
            g2d.drawImage(image1, 0, 0, null);
            if (horizontal) {
                g2d.drawImage(image2, image1.getWidth(), 0, null);
            } else {
                g2d.drawImage(image2, 0, image1.getHeight(), null);
            }

            // 释放资源
            g2d.dispose();

            // 保存新图片
            ImageIO.write(combinedImage, "jpg", new File("E:\\HuaweiMoveData\\Users\\wangyue\\Desktop\\jiajiao\\222.jpg"));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}